/*     */ package com.zimbra.cs.account.names;
/*     */ 
/*     */ import com.google.common.base.Strings;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import javax.mail.internet.AddressException;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class NameUtil
/*     */ {
/*     */   private static final int MAX_DOMAIN_NAME_LEN = 255;
/*     */   
/*     */   private static boolean isDot(int c)
/*     */   {
/*  32 */     if ((c == 46) || (c == 12290) || (c == 65294) || (c == 65377)) {
/*  33 */       return true;
/*     */     }
/*  35 */     return false;
/*     */   }
/*     */   
/*     */   private static boolean containsNonLDH(String input) {
/*  39 */     for (int i = 0; i < input.length(); i++) {
/*  40 */       int c = input.charAt(i);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  46 */       if (!isDot(c))
/*     */       {
/*     */ 
/*  49 */         if ((c <= 44) || ((c >= 46) && (c <= 47)) || ((c >= 58) && (c <= 64)) || ((c >= 91) && (c <= 96)) || ((c >= 123) && (c <= 127)))
/*     */         {
/*     */ 
/*     */ 
/*     */ 
/*  54 */           return true; }
/*     */       }
/*     */     }
/*  57 */     return false;
/*     */   }
/*     */   
/*     */   public static void validEmailAddress(String addr) throws ServiceException {
/*     */     try {
/*  62 */       InternetAddress ia = new InternetAddress(addr, true);
/*  63 */       if ((ia.getPersonal() != null) && (!ia.getPersonal().equals("")))
/*  64 */         throw ServiceException.INVALID_REQUEST("invalid email address", null);
/*     */     } catch (AddressException e) {
/*  66 */       throw ServiceException.INVALID_REQUEST("invalid email address", e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void validNewDomainName(String domain)
/*     */     throws ServiceException
/*     */   {
/*  80 */     if (domain.length() > 255) {
/*  81 */       throw ServiceException.INVALID_REQUEST("invalid domain name " + domain + ", domain name cannot exceed " + 255 + " chars", null);
/*     */     }
/*     */     
/*  84 */     String email = "test@" + domain;
/*     */     try {
/*  86 */       validEmailAddress(email);
/*     */     } catch (ServiceException e) {
/*  88 */       throw ServiceException.INVALID_REQUEST("invalid domain name " + domain, e);
/*     */     }
/*     */     
/*  91 */     Config config = Provisioning.getInstance().getConfig();
/*  92 */     boolean allowNonLDH = config.getBooleanAttr("zimbraAllowNonLDHCharsInDomain", true);
/*  93 */     if ((!allowNonLDH) && (containsNonLDH(domain))) {
/*  94 */       throw ServiceException.INVALID_REQUEST("invalid domain name " + domain + ": " + " containing non-LDH characters and " + "zimbraAllowNonLDHCharsInDomain" + " is false", null);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public static class EmailAddress
/*     */   {
/*     */     private String localPart;
/*     */     
/*     */     private String domain;
/*     */     
/*     */ 
/*     */     public EmailAddress(String addr)
/*     */       throws ServiceException
/*     */     {
/* 109 */       this(addr, true);
/*     */     }
/*     */     
/*     */     public EmailAddress(String addr, boolean strict) throws ServiceException {
/* 113 */       int index = addr.indexOf('@');
/* 114 */       if (index == -1) {
/* 115 */         this.localPart = addr;
/* 116 */         this.domain = null;
/*     */       } else {
/* 118 */         this.localPart = addr.substring(0, index);
/* 119 */         this.domain = addr.substring(index + 1);
/*     */       }
/*     */       
/* 122 */       if ((strict) && (Strings.isNullOrEmpty(this.domain))) {
/* 123 */         throw ServiceException.INVALID_REQUEST("must be valid email address: " + addr, null);
/*     */       }
/*     */     }
/*     */     
/*     */     public static String getAddress(String localPart, String domain) {
/* 128 */       return localPart + "@" + domain;
/*     */     }
/*     */     
/*     */     public static String getDomainNameFromEmail(String addr) throws ServiceException {
/* 132 */       EmailAddress email = new EmailAddress(addr);
/* 133 */       return email.getDomain();
/*     */     }
/*     */     
/*     */     public static String getLocalPartFromEmail(String addr) throws ServiceException {
/* 137 */       EmailAddress email = new EmailAddress(addr);
/* 138 */       return email.getLocalPart();
/*     */     }
/*     */     
/*     */     public String getLocalPart() {
/* 142 */       return this.localPart;
/*     */     }
/*     */     
/*     */     public String getDomain() {
/* 146 */       return this.domain;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/names/NameUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */